// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
/*eslint-disable*/
import Vue from 'vue'
import App from './App'
import Vuex from 'vuex'
import store from './store'
import router from './router'
import Mint from 'mint-ui'
import '../static/css/style.css'
import '../static/css/custom-theme.css'
import '../static/css/video-js.css'
import FastClick from 'fastclick'
import '../static/js/flexible.js'
// import wx from '../static/js/jweixin-1.6.0.js'
import VideoPlayer from 'vue-video-player'
import vueWechatTitle from 'vue-wechat-title'
import 'babel-polyfill'
import Es6Promise from 'es6-promise'
Es6Promise.polyfill()
Vue.use(VideoPlayer);
Vue.use(vueWechatTitle);
// Vue.use(wx);
// if (process.env.MOCK) {    // 判断是否为mock模式
//   require('./mock/index.js')
// }
/**
*监听浏览器点击返回前进操作动画
*浏览器端使用需要注意路由path的创建，二级应该在一级的基础上添加
*如一级/Home，则二级为/Home/Detail，依次往后加，如果是app的话可忽略以下代码
*/
let init = 0
window.addEventListener('popstate', function(e) {
  init++
  if (init < 2) {
    router.beforeEach((to, from, next) => {
      let arr1 = to.path.split('/')
      let arr2 = from.path.split('/')
      if (arr1.length === 2) {
        if (arr1[1].length === 0) {
          arr1.splice(1, 1)
        }
      }
      if (arr2.length === 2) {
        if (arr2[1].length === 0) {
          arr2.splice(1, 1)
        }
      }
      if (arr1.length < arr2.length) {
        router.togoback()
      } else {
        router.togoin()
      }
      next()
    })
  }
}, false)

// app 修改状态栏颜色
// document.addEventListener('plusready', function () {
//   let System = window.plus.os.name
//   if (System === 'iOS') {
//     window.plus.navigator.setStatusBarBackground('#d81e06')
//   }
// })

Vue.use(Mint)
Vue.use(Vuex)
Vue.config.productionTip = false

FastClick.prototype.focus = function (targetElement) {
  let length;
  if (targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
    length = targetElement.value.length;
    targetElement.focus();
    targetElement.setSelectionRange(length, length);
  } else {
    targetElement.focus();
  }
};
FastClick.attach(document.body)
// 判断ios还是android
Vue.prototype.isIosOrAndroid = function () {
  let u = navigator.userAgent;
  let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  let isStr = ''
  if (isAndroid) {
    isStr = 'android'
  }
  if (isiOS) {
    isStr = 'ios'
  }
  return isStr
}

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({     
  easing: 'ease',  // 动画方式    
  speed: 500,  // 递增进度条的速度    
  showSpinner: false, // 是否显示加载ico    
  trickleSpeed: 200, // 自动递增间隔    
  minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to, from, next) => {
  if (!window.initUrl) {
    window.initUrl = location.href.split('#')[0]
  }
  var user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {}
  if (to.name === 'story' || to.name === 'home') {
    to.meta.title = user['app_name']
  }
  // 每次切换页面时，调用进度条
  NProgress.start();
　　// 若加载时间长且不定，担心进度条走完都没有加载完，可以调用
　NProgress.inc(); //这会以随机数量递增，且永远达不到100%，也可以设指定增量
  next();

})
//当路由进入后：关闭进度条
router.afterEach(() => {  
  // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
